package com.atguigu.gulimall.order;

import org.apache.commons.codec.binary.Hex;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;


/**
 * @NAME: CryptoUtils
 * @USER: wjj
 * @DATE: 2023/11/24
 * @TIME: 12:41
 * @DAY_NAME_FULL: 星期五
 * @PROJECT_NAME: gulimall
 */
public class CryptoUtils {
    public static void main(String[] args) throws UnsupportedEncodingException {
        long time = 1234566789L;
        System.out.println(time);
        Long timestmp = time/1000;
        System.out.println(timestmp);

        RestTemplate restTemplate=new RestTemplate();//redis提供发送请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String json = "{\n" +
                "    \"msgSignature\": \"0a8f22f5a90d9912426225ccee351845542e890f\",\n" +
                "    \"timeStamp\": \"1702005287\",\n" +
                "    \"nonce\": \"aNUG205pP4hFwDAD5sFeAEBFrX3f9ovf\",\n" +
                "    \"encrypt\": \"SmfqFMFrQZNAE8PiQLhinFuIHoNG5VAP8elryCHHtBNN/81Tgwa86SCn4hnYBt6jV66/1FBccoIImsqZFj8LpY4pGw1eczEGvUMVm9K0RnY=\"}";
        HttpEntity<String> request1 = new HttpEntity<>(json, headers);
        ResponseEntity<String> entity = restTemplate.postForEntity("http://10.229.2.94:8282/decrypt_demo.php?app_secret=lbNTQ5Prug7g1EPBe8bavp3CUGJCuBZr",
                request1, String.class);
        String body = entity.getBody();
        System.out.println(body);
    }

    public void test1() throws UnsupportedEncodingException {
        // 获取秒级时间戳转成字符串
        long time = new Date().getTime();
        System.out.println(time);
        Long timestmp = time/1000;
        System.out.println(timestmp);
        String timemiao = timestmp.toString();
        System.out.println(timemiao);
        String string = UUID.randomUUID().toString();
        // 拼接加密
        String message = string + timemiao + "ZhbxD3a3obHt7IA1dIPGylPSMY4OMers";
        byte[] bytes = message.getBytes("UTF-8");
        String hash_object = getSHA256Str(message);
        System.out.println("hash_sign");
        System.out.println(hash_object);

    }
    /***
     * 利用Apache的工具类实现SHA-256加密
     * @param str 加密后的报文
     * @return
     */
    public static String getSHA256Str(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }
    public void test2(){
// 获取秒级时间戳转成字符串
        long time = new Date().getTime();
        System.out.println(time);
        Long timestmp = time/1000;
        System.out.println(timestmp);
        String timemiao = timestmp.toString();
        System.out.println(timemiao);
// 拼接加密
        String message = "1234" + timemiao + "ZhbxD3a3obHt7IA1dIPGylPSMY4OMers";
        MessageDigest messageDigest;
        String sign = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(message.getBytes("UTF-8"));
            sign = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }



}
